package descentFly.app
{
	import UIFree.ObjectEvent;
	import UIFree.uif_Timer;
	import UIFree.uif_component;
	
	import descentFly.app.dialogs.df_alert;
	import descentFly.app.dialogs.df_question;
	import descentFly.app.dialogs.df_sceneLoadProgress;
	
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.external.ExternalInterface;
	import flash.geom.Rectangle;
	import flash.ui.Mouse;
	
	public class df_baseInterface extends df_modalLayer {
		private var _alert: df_alert;
		private var _sceneComponents: df_component;
		private var _sceneLoadProgress: df_sceneLoadProgress;
		
		public function df_baseInterface(parent:uif_component, ax:int=0, ay:int=0, awidth:int=0, aheight:int=0, styleClass:Object='') {
			super(parent, ax, ay, awidth, aheight, styleClass);
			focusThisSwf();			
		}
		
		override protected function createComponents(layer:df_component, inner:Rectangle):void {
			super.createComponents(layer, inner);
			_sceneComponents = new df_component(layer, inner.x, inner.y, inner.width, inner.height);
			_sceneLoadProgress = new df_sceneLoadProgress(this);
		}
		
		protected function get sceneComponents(): df_component {
			return _sceneComponents;
		}
		
		override protected function listenEvents():void {
			super.listenEvents();
			addEvent(df_events.ALERT, function(e: ObjectEvent): void {
				showAlert(e.object as String);
			});
			
			addEvent(df_events.LOADSCENESTART, doLoadSceneStart, Space);
			addEvent(df_events.LOADSCENEEND, doLoadSceneEnd, Space);
			
			addEvent(df_events.ALERT, function(e: ObjectEvent): void {
				showAlert(e.object as String);
			}, Space);		
			
			addEvent(df_events.QUESTION, function(e: ObjectEvent): void {
				showQuestion(e.object.question, e.object.callback);
			}, Space);
			
			stage.addEventListener(KeyboardEvent.KEY_UP, doKeyUp);
			ExternalInterface.addCallback('enableSceneEI', function (): void {
				focusThisSwf();
			});
			ExternalInterface.addCallback('disableSceneEI', function (): void {
				Space.disableScene = true;
			});
			ExternalInterface.addCallback('fireEventEI', function (event: String): void {
				dispatchEvent(new Event(event));
			});
		}
		
		override protected function changeModalMode(a_modal: Boolean): void {
			super.changeModalMode(a_modal);
			Space.disableScene = a_modal;
			checkAndSetMouseShow(a_modal);
		}
		
		private function doKeyUp(e: KeyboardEvent): void {
			if ((e.keyCode == 27) && (!modalCount())) {
				//this.Space.disableScene = true;
				ExternalInterface.call("disableSceneJS");
			}/* else if (e.keyCode == 13) {
				Space.dispatchEvent(new ObjectEvent(df_events.QUESTION, {
					question: 'ede',
					callback: function(ok: Boolean): void {
						
					}
				}));
			}*/
		}
		
		protected function checkAndSetMouseShow(a_set: Boolean): void {
			if (Space.plockSupport) {
				if (a_set) Mouse.show();
				else Mouse.hide();
			}
		}
		
		private function focusThisSwf():void {
			uif_Timer.one(500, function(): void {
				ExternalInterface.call("focusGame");
				Space.disableScene = modalCount() > 0;
				checkAndSetMouseShow(modalCount() > 0);
			});
		}
		
		protected function showAlert(message: String): df_alert {
			if (message) {
				function doClose(e: Event): void {
					_alert = null;
				}
				if (!_alert) {
					_alert  = new df_alert(this, message);
					_alert.show();
					_alert.addEvent(df_window.CLOSE, doClose);
				}
			}
			return _alert;			
		}
		
		protected function showQuestion(message: String, retFunc: Function): void {
			(new df_question(this, message, retFunc)).show();
		}
		
		protected function doLoadSceneStart(e: Event): void {
			_sceneComponents.visible = false;
			_sceneLoadProgress.show();
		}
		
		protected function doLoadSceneEnd(e: Event): void {
			_sceneComponents.visible = true;
			_sceneLoadProgress.close();
		}
	}
}